/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.wms.service;

import java.text.DecimalFormat;
import java.util.List;

import com.jeeplus.modules.infor.entity.Goods;
import com.jeeplus.modules.infor.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.wms.entity.StorageInform;
import com.jeeplus.modules.wms.mapper.StorageInformMapper;
import com.jeeplus.modules.wms.entity.MaterialParticulars;
import com.jeeplus.modules.wms.mapper.MaterialParticularsMapper;
import com.jeeplus.modules.wms.entity.Statistics;
import com.jeeplus.modules.wms.mapper.StatisticsMapper;

/**
 * 入库通知Service
 * @author sld
 * @version 2017-12-05
 */
@Service
@Transactional(readOnly = true)
public class StorageInformService extends CrudService<StorageInformMapper, StorageInform> {

	@Autowired
	private MaterialParticularsMapper materialParticularsMapper;
	@Autowired
	private StatisticsMapper statisticsMapper;

	@Autowired
	private GoodsService service;

	
	public StorageInform get(String id) {
		StorageInform storageInform = super.get(id);
		storageInform.setMaterialParticularsList(materialParticularsMapper.findList(new MaterialParticulars(storageInform)));
		storageInform.setStatisticsList(statisticsMapper.findList(new Statistics(storageInform)));
		return storageInform;
	}
	
	public List<StorageInform> findList(StorageInform storageInform) {
		return super.findList(storageInform);
	}
	
	public Page<StorageInform> findPage(Page<StorageInform> page, StorageInform storageInform) {
		return super.findPage(page, storageInform);
	}
	
	@Transactional(readOnly = false)
	public void save(StorageInform storageInform) {
		super.save(storageInform);
		for (MaterialParticulars materialParticulars : storageInform.getMaterialParticularsList()){
			if (materialParticulars.getId() == null){
				continue;
			}
			if (MaterialParticulars.DEL_FLAG_NORMAL.equals(materialParticulars.getDelFlag())){
				if (StringUtils.isBlank(materialParticulars.getId())){
					materialParticulars.setState("未入库");
					materialParticulars.setQuantitiesReceived("0");
					materialParticulars.setStorageInform(storageInform);
					materialParticulars.preInsert();
					materialParticularsMapper.insert(materialParticulars);
				}else{
					materialParticulars.preUpdate();
					materialParticularsMapper.update(materialParticulars);
				}
			}else{
				materialParticularsMapper.delete(materialParticulars);
			}
		}
		for (Statistics statistics : storageInform.getStatisticsList()){
			if (statistics.getId() == null){
				continue;
			}
			if (Statistics.DEL_FLAG_NORMAL.equals(statistics.getDelFlag())){
				if (StringUtils.isBlank(statistics.getId())){
					statistics.setStorageInform(storageInform);
					statistics.setType(storageInform.getType());
					statistics.setPro(storageInform.getProjectName());
					statistics.preInsert();
					statisticsMapper.insert(statistics);
				}else{
					statistics.preUpdate();
					statisticsMapper.update(statistics);
				}
			}else{
				statisticsMapper.delete(statistics);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(StorageInform storageInform) {
		super.delete(storageInform);
		materialParticularsMapper.delete(new MaterialParticulars(storageInform));
		statisticsMapper.delete(new Statistics(storageInform));
	}
	
}